﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DMHelper.Models
{
    public class OrdreInitiative
    {
        private static SortedList<int, InfosOrdreInitiative> temp = new SortedList<int, InfosOrdreInitiative>();
        private static SortedList<int, InfosOrdreInitiative> listeIni = new SortedList<int, InfosOrdreInitiative>();

        public static void ajouter(string name, int ini)
        {
            listeIni.Add(listeIni.Count + 1, new InfosOrdreInitiative(name, ini));
            SortList();
        }

        public static void ajouterTemp(string name, int ini)
        {
            temp.Add(temp.Count + 1, new InfosOrdreInitiative(name, ini));
        }

        private static void SortList()
        {
            int higherIni = 0;
            int position = 0;

            if (listeIni.Count > 0)
            {
                foreach (var item in listeIni)
                {
                    if (item.Value.getIni() >= higherIni)
                    {
                        higherIni = item.Value.getIni();
                        position = listeIni.IndexOfKey(item.Key);
                    }
                }
                ajouterTemp(listeIni.ElementAt(position).Value.getName(), listeIni.ElementAt(position).Value.getIni());
                listeIni.RemoveAt(position);
                SortList();
            }
            else
            {
                listeIni = new SortedList<int,InfosOrdreInitiative>(temp);
                temp.Clear();
            }
        }

        public static void supprimer(int position)
        {
            listeIni.Remove(position);
            SortList();
        }

        public static void supprimerTout()
        {
            listeIni.Clear();
        }

        public static SortedList<int, InfosOrdreInitiative> liste()
        {
            return listeIni;
        }

        public static void modifierIni(string ini)
        {
            int count = 1;
            string modifIni = "";
            int index = -100;

            foreach (char i in ini)
            {
                if (!i.Equals(','))
                {
                    if (index == -100)
                    {
                        index = count;
                    }
                    modifIni = modifIni + i;
                }
                else
                {
                    count++;
                }
            }

            listeIni[index].setIni(Convert.ToInt32(modifIni));
            SortList();

        }

        public static void supprimerString(string btnSubmit)
        {
            string number = btnSubmit.Substring(10);
            supprimer(Convert.ToInt32(number));
        }
    }

    public class InfosOrdreInitiative
    {
        private string name;
        private int initiative;

        public InfosOrdreInitiative(string name, int initiative)
        {
            this.name = name;
            this.initiative = initiative;
        }

        public string getName()
        {
            return name;
        }

        public int getIni()
        {
            return initiative;
        }


        internal void setIni(int initiative)
        {
            this.initiative = initiative;
        }
    }

}
